mod g01_client;
mod handler;
pub mod ice;
mod message;
pub mod rrai;
pub mod webrtc;

use self::message::SignalMessage;
use std::{collections::HashMap, sync::RwLock};
use tokio::sync::mpsc::{Receiver, UnboundedReceiver, UnboundedSender};

pub use g01_client::*;
pub use message::*;
pub use rrai::request_family;

pub struct G01Client {
    pub sender: UnboundedSender<String>,
}

lazy_static::lazy_static! {

    pub(crate) static ref SIGNALING_CLIENT: RwLock<Option<G01Client>> = RwLock::new(None);
}

pub use g01_client::connect_g01_signaling;

pub fn signaling_is_login() -> anyhow::Result<bool> {
    let client = SIGNALING_CLIENT
        .read()
        .map_err(|err| anyhow::anyhow!("获取Client失败!{}", err))?;
    Ok(client.is_some())
}

pub async fn close_and_remove(token: String) -> anyhow::Result<()> {
    {
        let mut client = SIGNALING_CLIENT
            .write()
            .map_err(|err| anyhow::anyhow!("获取Client失败!{}", err))?;

        *client = None;
    }
    {
        // g01_client::connect_g01_signaling(&token).await?;
    }
    Ok(())
}
